package com.qishon.pc.domain.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * Created by xm on 2017/1/17.
 */
@Slf4j
public class SplitUtil
{

//    public static void main(String str[]) throws IOException
//    {
//        SplitTools();
//    }

    public void SplitTools(String runPath,String imageTypeNum,String imageTypes,String imageTypesheights,String taskName) throws IOException
    {
        List<String> OriginPath = new ArrayList<String>();
        List<String> NameSuffixs = new ArrayList<String>();
        List<String> Heights = new ArrayList<String>();
        String ExePath = runPath+"\\12.SplitTools\\nconvert.exe";
        String sourcepath = runPath + "\\04.Joint"+"\\"+taskName;
        String savepath = runPath + "\\04.Joint"+"\\"+taskName;
        String fileSuffix = "png";
        log.info("runPath:"+runPath);
        log.info("imageTypeNum:"+imageTypeNum);
        log.info("imageTypes:"+imageTypes);
        log.info("imageTypesheights:"+imageTypesheights);
        if(AnalyseValue(NameSuffixs,Heights,imageTypeNum,imageTypes,imageTypesheights))
        {
            FindFolder(sourcepath,OriginPath);
            SplitToolsCmd(ExePath,OriginPath,savepath,fileSuffix,NameSuffixs,Heights);
            
        }
    }

    public boolean AnalyseValue(List<String> suffixs,List<String> heights,String imageTypeNum,String imageTypes,String imageTypesheights)
    {
        String[] images = imageTypes.split(",");
        for (int i =0; i< images.length;i++){
            suffixs.add(images[i]);
        }
        log.info("Suffixs.size():"+suffixs.size());
        String[] imagesHeights = imageTypesheights.split(",");
        for (int j =0; j< imagesHeights.length;j++){
            heights.add(imagesHeights[j]);
        }
        int imageNumber = Integer.parseInt(imageTypeNum);
        if(suffixs.size()!= imageNumber || heights.size() != imageNumber)
        {
            //介个就是说参数有毛病的情况，校验
            log.info("AnalyseValue:failed");
            log.info("Suffixs.size():"+suffixs.size());
            log.info("Heights.size():"+heights.size());
            log.info("imageNumber:"+imageNumber);
            return false;
        }
        else
        {
            log.info("AnalyseValue:SUCCESS");
            return true;
        }
    }

    public static void FindFolder(String sourcepath,List<String> OriginPath)
    {
        log.info("FindFolder start");
        File f = new File(sourcepath);
        File[] files =f.listFiles();
        if (files != null)
        {
            for(File file:files)
            {
                if(file.isDirectory())
                {
                    FindFolder(file.getPath(),OriginPath);
                }
                else
                {
                    OriginPath.add(file.getPath().toString());
                }
            }
        }
        log.info("FindFolder over");
    }

    public static void SplitToolsCmd(String ExePath,List<String> OriginPath, String SavePath,String fileSuffix,List<String> NameSuffixs,List<String> Heights) throws IOException
    {
        log.info("SplitToolsCmd start");
        String OriginImagePath;
        String SaveImageName,SaveImageHeight,SaveImageWidth;
        File dir = new File(SavePath);
        List<File> FileNames = new ArrayList<File>();
        FileInputStream fileInputStream = new FileInputStream(new File(OriginPath.get(0)));
        BufferedImage BuffImg = ImageIO.read(fileInputStream);//默认所有图片尺寸一样
        int BuffImgH = BuffImg.getHeight();
        int BuffImgW = BuffImg.getWidth();
        boolean renameSuccess = false;
        fileInputStream.close();
        if(!dir.exists())
        {
            dir.mkdirs();
        }

        log.info("OriginPath.size():"+OriginPath.size());
        for(int i = 0; i < OriginPath.size(); i++)
        {
            OriginImagePath = OriginPath.get(i);
            log.info("[[]]now_i:"+i);
            log.info("OriginImagePath:"+OriginImagePath);
            if(OriginImagePath.endsWith(fileSuffix))
            {
                FileNames.clear();
                //oldfile = new File(OriginImagePath);
                FileNames.add(new File(OriginImagePath));
                String nametemp = OriginImagePath.substring(0,(OriginImagePath.length()-fileSuffix.length()-1));
                log.info("nametemp:"+nametemp);
                Process child;
                BufferedReader buf;
                String str;
                //for(String nameSuffix:NameSuffixs)
                log.info("NameSuffixs.size():"+NameSuffixs.size());
                for(int j = 0;j < NameSuffixs.size(); j++)
                {
                    SaveImageName = nametemp + "_" + NameSuffixs.get(j) +"."+fileSuffix;
                    log.info("SaveImageName:"+SaveImageName);
                    SaveImageHeight = Heights.get(j);
                    log.info("SaveImageHeight:"+SaveImageHeight);
                    SaveImageWidth = Integer.toString(Integer.parseInt(SaveImageHeight) * BuffImgW/BuffImgH);
                    log.info("SaveImageWidth:"+SaveImageWidth);
                    String commons[] = {ExePath,"-out",fileSuffix,"-o",SaveImageName,"-resize",SaveImageWidth,SaveImageHeight,OriginImagePath};
                    child = Runtime.getRuntime().exec(commons);
                    buf = new BufferedReader(new InputStreamReader(child.getErrorStream()));
                    str = "";
                    while((str = buf.readLine())!= null)
                    {
                        log.info("Systemprints:");
                        System.out.println(str);
                    }
                }

                /*
                SaveImageName = OriginImagePath.substring(0,(OriginImagePath.length()-fileSuffix.length()-1))+newNameSuffix+"."+fileSuffix;
                String commons[] = {ExePath,"-out",fileSuffix,"-o",SaveImageName,"-resize",picHight,picWidth,OriginImagePath};
                Process child = Runtime.getRuntime().exec(commons);
                BufferedReader buf = new BufferedReader(new InputStreamReader(child.getErrorStream()));
                String str = "";
                while((str = buf.readLine())!= null)
                {
                    System.out.println(str);
                }/**/
                //renaame part
                /*
                newfile = new File(OriginImagePath.substring(0,(OriginImagePath.length()-fileSuffix.length()-1))+oldNameSuffix+"."+fileSuffix);
                renameSuccess = oldfile.renameTo(newfile);
                if(!renameSuccess)
                {
                    System.out.println("rename file" + OriginImagePath + "failed");
                }/**/

            }
            else
            {
                log.info("file not end with "+fileSuffix);
                log.info("OriginImagePath:"+OriginImagePath);
            }
        }

        log.info("SplitToolsCmd over");
    }
}
